import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_plans_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/emergency_plan/common/emergency_plan_common_widget.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/task_emergency/task_emergency_plan/task_emergency_plan_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 我的任务-应急预案
class TaskEmergencyPlanPage extends StatelessWidget {
  TaskEmergencyPlanPage({super.key});

  final controller = Get.put(TaskEmergencyPlanController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          appBar: CustomAppBar(titleText: '应急预案'),
          key: controller.popRightScaffoldKey,
          endDrawer: Container(
            width: AppDimens.sw - 75.w,
            height: AppDimens.sh,
            color: Colors.white,
            child: _getFilterWidget(),
          ),
          body: Container(
            color: const Color(0xffF5F5F6),
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
            child: Column(children: [
              _getSearchConditionWidget(),
              AppGaps.vGap12,
              Expanded(
                  child: CustomEasyRefreshList<EmergencyPlansEntity>(
                key: controller.globalKey,
                dataLoad: (pageIndex, pageSize, {String? search}) async {
                  return controller.getListData(pageIndex, pageSize);
                },
                itemBuilder: (EmergencyPlansEntity item, int index) {
                  return CustomTitleChartPage(
                    title: item.emergencyPlanName,
                    // titleRightWidget: _getStateView(item.status!),
                    titleRightWidget: EmergencyPlanCommonWidget.getStateView(item.toExamineStatus),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        CustomTextWidget.getTextRowView(title: '预案类别：', value: item.planTypeName),
                        AppGaps.vGap12,
                        CustomTextWidget.getTextRowView(title: '预案级别：', value: item.emergencyPlanLevelName),
                        AppGaps.vGap12,
                        CustomTextWidget.getTextRowView(title: '适用事故类型：', value: item.probationAccidentNames),
                        Visibility(
                          visible: item.toExamineStatus == 2 || item.toExamineStatus == 3,
                          child: Column(
                            children: [
                              AppGaps.vGap12,
                              _getApprovalButton(item),
                            ],
                          ),
                        )
                      ],
                    ),
                  ).clickOnce(() {
                    Get.toNamed(AppRoutes.emergencyPlanDetails, arguments: {'id': item.id, 'status': item.toExamineStatus});
                  });
                },
              ))
            ]),
          ),
        );
      },
    );
  }

  /// 获取右侧筛选样式
  Widget _getFilterWidget() {
    return CustomFilterRightSheet(
      dataList: controller.allFilterList,
      onConfirm: controller.screenConfirm,
      isRadio: true,
    );
  }

  /// 获取审批按钮
  Widget _getApprovalButton(EmergencyPlansEntity item) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          '操作:',
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(10.w, 6.w, 6.w, 6.w),
          decoration: BoxDecoration(
            color: const Color(0xFFE5E7FF),
            borderRadius: BorderRadius.circular(4.w),
          ),
          child: Row(
            children: [
              Text(
                '审批',
                style: TextStyle(
                  fontSize: 13.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff5C6DFF),
                ),
              ),
              AppGaps.hGap2,
              SvgPicture.asset(
                AppImages.icon_right_arrow,
              ),
            ],
          ),
        ).clickOnce(() {
          controller.clickOnApproval(item);
          // Get.toNamed(AppRoutes.emergencyPlanApproval, arguments: item.id);
        }),
      ],
    );
  }

  ///获取条件搜索项
  Widget _getSearchConditionWidget() {
    return Row(
      children: [
        Expanded(
            child: CustomSearchEnter(
          height: 32.w,
          hintText: '请输入名称',
          text: controller.search,
          onCallBack: (value) {
            controller.search = value;
            controller.refreshPage();
          },
        )),
        AppGaps.hGap8,
        _screen(),
      ],
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).clickOnce(controller.screen);
  }
}
